package rentcar.datasender;

import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.boot.autoconfigure.*;
import rentcar.datasender.models.CmdInfo;
import rentcar.datasender.models.CmdResultInfo;
import rentcar.datasender.models.TelematicsPara;
import rentcar.datasender.models.TelematicsResult;
import rentcar.datasender.services.DataGenerator;

@SpringBootApplication
public class Application {

        private static final int port = 9876;
       // private static final String ip = "127.0.0.1";
private static final String ip ="54.222.212.110";

        public static void main(String[] args) {
                try {
                        // 创建Socket 
                        // 创建一个流套接字并将其连接到指定 IP 地址的指定端口号(本处是本机)
                        Socket socket = new Socket(ip, port);
                        // 60s超时
                        socket.setSoTimeout(60000);
                        PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
                        DataGenerator dg = new DataGenerator();

                        Integer total = 1;
                        for (int i = 0; i < total; i++) {
                                System.out.println("sending message to "+ip+" : "+port+"......");
                                // 发送客户端准备传输的信息  
                                // 由Socket对象得到输出流，并构造PrintWriter对象
                                // 将输入读入的字符串输出到Server
                                //BufferedReader sysBuff = new BufferedReader(new InputStreamReader(System.in));
                                //printWriter.println(sysBuff.readLine());
                                printWriter.println(dg.GetMsg());
                                // 刷新输出流，使Server马上收到该字符串
                                printWriter.flush();
                                Thread.sleep(1000);
                        }
                        
                        
                        Integer count = 0;
                        Gson gson = new Gson();
                        while (true) {
                                // 用于获取服务端传输来的信息
                                // 由Socket对象得到输入流，并构造相应的BufferedReader对象
                                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                                // 输入读入一字符串
                                String result = bufferedReader.readLine();
                              //  System.out.println("Got message: "+result);
                                if (result != null && !"".equals(result)) {
                                        CmdInfo tp = gson.fromJson(result, CmdInfo.class);
                                        System.out.println("Server say : " + result);

                                        String strResult=GetResult(tp.getSystemno());
                                        System.out.println("The result is: "+strResult);
                                        printWriter.println(strResult);
                                        // 刷新输出流，使Server马上收到该字符串
                                        printWriter.flush();
                                        count++;
                                        if (count > 2) {
                                               break;
                                       }
                                }
                                //break;
                        }

                        // 关闭Socket 
                        printWriter.close();
                        //bufferedReader.close();
                        socket.close();
                } catch (Exception e) {
                        System.out.println("Exception:" + e);
                }
        }

        static String GetResult(String vin) {
                TelematicsResult tr = new TelematicsResult();
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                tr.setTime(df.format(new Date()));
                tr.setResult(0);

                CmdResultInfo tp = new CmdResultInfo();
                tp.setSystemno(vin);
                tp.setIdentifying("E03");
                tp.setData(tr);

                Gson gson = new Gson();
                return gson.toJson(tp);
        }

}
